/*
 * preferencesdialog.cpp - preferences dialog
 * Copyright (C) 2009  Sergei Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Kajim::ChatInputText widget implements basic functions required
 * by chat text entry widget: history navigation, autocompletion etc
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "preferencesdialog.h"
#include "constants.h"
#include "settings.h"
#include "windowstore.h"
#include "ui/ui_preferences.h"

struct PreferencesDialog::Private
{
    Ui::PreferencesDialog ui;
};

PreferencesDialog::PreferencesDialog(QWidget * parent)
    : QDialog(parent)
{
    d = new Private;

    d->ui.setupUi(this);

    initControls();
}

PreferencesDialog::~PreferencesDialog()
{
    delete d;
}

void PreferencesDialog::accept()
{
    const QAbstractButton * checkedButton = d->ui.buttonGroup->checkedButton();
    QString layout_mode = Kajim::Ui::LAYOUT_MULTIPLE_WINDOWS;
    if (checkedButton == d->ui.multipleRadioButton) {
        layout_mode = Kajim::Ui::LAYOUT_MULTIPLE_WINDOWS;
    } else if (checkedButton == d->ui.singleRadioButton) {
        layout_mode = Kajim::Ui::LAYOUT_SINGLE_WINDOW;
    } else if (checkedButton == d->ui.chatAndRosterRadioButton) {
        layout_mode = Kajim::Ui::LAYOUT_ROSTER_AND_SINGLE_CHAT;
    }

    Kajim::Settings::set("ui/layout", layout_mode);
    QDialog::accept();
    Kajim::WindowStore * window_store = Kajim::WindowStore::instance();
    window_store->updateLayout();
}

void PreferencesDialog::initControls()
{
    QString layout_mode = Kajim::Settings::get("ui/layout").toString();

    if (Kajim::Ui::LAYOUT_MULTIPLE_WINDOWS == layout_mode) {
        d->ui.multipleRadioButton->setChecked(true);
    } else if (Kajim::Ui::LAYOUT_ROSTER_AND_SINGLE_CHAT == layout_mode) {
        d->ui.chatAndRosterRadioButton->setChecked(true);
    } else if (Kajim::Ui::LAYOUT_SINGLE_WINDOW == layout_mode) {
        d->ui.singleRadioButton->setChecked(true);
    } else {
        d->ui.multipleRadioButton->setChecked(true);
    }
}
